package com.datatools.ui.dataview;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.io.Serializable;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Vector;

/**
 * 数据表格
 */
public class DataGrid extends JTable implements Serializable {
    private static final long serialVersionUID = 1L;

    public DataGrid() {
        super();
    }

    public DataGrid(TableModel dm) {
        super(dm);

    }

    public DataGrid(TableModel dm, TableColumnModel cm) {
        super(dm, cm);
    }

    public DataGrid(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
        super(dm, cm, sm);
    }

    public DataGrid(int numRows, int numColumns) {
        super(numRows, numColumns);
    }

    public DataGrid(Vector rowData, Vector columnNames) {
        super(rowData, columnNames);
    }

    public DataGrid(Object[][] rowData, Object[] columnNames) {
        super(rowData, columnNames);
    }

    /**
     * 根据基偶行，设置不同颜色
     */
    public void paintRow() {
        TableColumnModel tcm = this.getColumnModel();
       for (int i = 0, n = tcm.getColumnCount(); i < n; i++) {
           TableColumn tc = tcm.getColumn(i);
           tc.setCellRenderer(new RowRenderer());
       }
    }
     /**
      * 定义内部类，用于控制单元格颜色，每两行颜色相间，本类中定义为蓝色和绿色。
      *
      * @author Sidney
      *
      */
     public class RowRenderer extends DefaultTableCellRenderer {

         public Component getTableCellRendererComponent(JTable jtable, Object value,
                     boolean isSelected, boolean hasFocus, int row, int column) {
             //设置奇偶行的背景色，可在此根据需要进行修改
             if (row % 2 == 0) {
                 setBackground(new Color(240,245,250));
             }else {
                 setBackground(Color.white);
             }
             return super.getTableCellRendererComponent(jtable, value, isSelected,
                  hasFocus, row, column);
         }
     }

    /**
    * 根据数据内容自动调整列宽。
    **/
     public void fitTableColumns() {
          this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
          JTableHeader header = this.getTableHeader();
          int rowCount = this.getRowCount();
          Enumeration columns = this.getColumnModel().getColumns();
          while(columns.hasMoreElements()) {
              TableColumn column = (TableColumn)columns.nextElement();
              int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
              int width = (int)header.getDefaultRenderer().getTableCellRendererComponent
              (this, column.getIdentifier(), false, false, -1, col).getPreferredSize().getWidth();
              for(int row = 0; row < rowCount; row++) {
                  int preferedWidth = (int)this.getCellRenderer(row, col).getTableCellRendererComponent
                  (this, this.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
                  width = Math.max(width, preferedWidth);
              }
              // 此行很重要
              header.setResizingColumn(column);
              column.setWidth(width + this.getIntercellSpacing().width+10);
          }
     }
    /**
     * 让JTable 出现横向滚动条，宽度根据数据列数自动自适应
     * @return
     */
    @Override
    public boolean getScrollableTracksViewportWidth() {
        if (autoResizeMode != AUTO_RESIZE_OFF) {
            if (getParent() instanceof JViewport) {
                return (((JViewport) getParent()).getWidth() > getPreferredSize().width);
            }
        }
        return false;
    }

}
